package com.dingyh.shopwebbackground.service.impl;

import com.dingyh.shopwebbackground.entity.Banned;
import com.dingyh.shopwebbackground.entity.User;
import com.dingyh.shopwebbackground.mapper.BannedMapper;
import com.dingyh.shopwebbackground.mapper.UserMapper;
import com.dingyh.shopwebbackground.service.BannedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingyh.shopwebbackground.util.CommonResult;
import com.dingyh.shopwebbackground.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dingyuhan
 * @since 2023-02-16
 */
@Service
public class BannedServiceImpl extends ServiceImpl<BannedMapper, Banned> implements BannedService {
    @Autowired
    BannedMapper bannedMapper;

    @Autowired
    UserMapper userMapper;

    public List<Banned> allList(Map<String,Object> map){
        return baseMapper.allList(map);
    }

    public List<Banned> selectByUseId(Map<String,Object> map){
            return bannedMapper.selectByUseId(map);
    }


    @Transactional(rollbackFor = Exception.class)
    public CommonResult sveBanned(String userid, String reason, String disable) {
        Banned banned = Banned.builder()
                .id(UUIDUtil.generator())
                .userid(userid)
                .timeadd(System.currentTimeMillis())
                .reason(reason)
                .disable(disable)
                .build();
        boolean result = save(banned);
        if (result) {
            User user = User.builder()
                    .id(userid)
                    .disable(banned.getDisable()).build();
            userMapper.updateById(user);
            return new CommonResult(200, "禁言成功", null);
        } else {
            return new CommonResult(500, "禁言失敗", null);
        }
    }

}
